package com.cuitrip.business.tripservice;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.component.item.CombinationItemView;
import com.cuitrip.service.R;
import com.cuitrip.util.SpanUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/19.
 */
public class CreateTripDescHighlightsFragment extends CustomUiFragment {
    @Bind(R.id.ll_container)
    LinearLayout container;

    @Bind(R.id.ll_item_container)
    LinearLayout itemContainer;

    @Bind(R.id.desc)
    TextView desc;
    private CreateTripDescFragment.OnResultCallback onResultCallback;
    private StringBuilder builder=new StringBuilder();
    private ServiceInfo serviceInfo;
    private boolean isDel=true;

    public static CreateTripDescHighlightsFragment newInstance(ServiceInfo serviceInfo){
        CreateTripDescHighlightsFragment createTripDescHighlightsFragment = new CreateTripDescHighlightsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ServiceInfo.KEY,serviceInfo);
        createTripDescHighlightsFragment.setArguments(bundle);
        return createTripDescHighlightsFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle= CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.trip_attribute_highlight);
        config.mRightStyle= CustomUiConfig.AreaStyle.TEXT_STYLE;
        config.mTopbarRightAreaName=getString(R.string.operation_delete);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_create_desc_highlight);
        if (getArguments()!=null){
            serviceInfo = (ServiceInfo) getArguments().getSerializable(ServiceInfo.KEY);
        }
        return view;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this,getView());
        if (serviceInfo!=null){
            String highLight = serviceInfo.getHighLight();
            if (!TextUtils.isEmpty(highLight)) {
                itemContainer.removeAllViews();
                String[] highLightArr = highLight.split("\n");
                for (int i = 0; i < highLightArr.length; ++i) {
                    CombinationItemView combinationItemView = new CombinationItemView(getActivity());
                    combinationItemView.setContent(highLightArr[i]);
                    itemContainer.addView(combinationItemView);
                }
            }

        }
    }

    @Override
    public void initListener() {
        SpannableStringBuilder epJump;
        if(serviceInfo.getCateList()==null || serviceInfo.getCateList().size()==0){
            epJump = SpanUtil.getEPJump(getString(R.string.trip_content_highlight_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.HIGHLIGHT_TYPE, "");
        }else {
            epJump = SpanUtil.getEPJump(getString(R.string.trip_content_highlight_desc_2), getString(R.string.trip_content_intro_see_example), getHostActivity(), SpanUtil.HIGHLIGHT_TYPE, serviceInfo.getCateList().get(0).getCategory());
        }
        desc.setText(epJump);
        desc.setClickable(true);
        desc.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onTopbarRightPress() {
        if(isDel){
            mTopbarRightArea.setText(R.string.operation_finish);
            isDel=false;
            if (itemContainer.getChildCount()!=0){
                int childCount = itemContainer.getChildCount();
                for (int i=0; i<childCount;++i){
                    CombinationItemView item  = (CombinationItemView) itemContainer.getChildAt(i);
                    item.setTrashIconVisible(true);

                }
            }
        }else{
            mTopbarRightArea.setText(R.string.operation_delete);
            if (itemContainer.getChildCount()!=0){
                int childCount = itemContainer.getChildCount();
                for (int i=0; i<childCount;++i){
                    CombinationItemView item  = (CombinationItemView) itemContainer.getChildAt(i);
                    item.setTrashIconVisible(false);
                }
            }
            isDel=true;
        }
    }

    @OnClick(R.id.tv_add)
    public void addItem(){
        CombinationItemView combinationItemView = new CombinationItemView(getActivity());
        combinationItemView.setTextHint(getString(R.string.trip_content_highlight_desc_1));
        itemContainer.addView(combinationItemView);
    }

    private void saveAction(){
        int childCount = itemContainer.getChildCount();
        for (int i=0;i<childCount;i++){
            CombinationItemView item  = (CombinationItemView) itemContainer.getChildAt(i);
            String content = item.getContent();
            if (!TextUtils.isEmpty(content)){
                if (i!=childCount-1){
                    builder.append(content+"\n");
                }else{
                    builder.append(content);
                }
            }

        }
        serviceInfo.setHighLight(builder.toString());
        onResultCallback.onResultBack(serviceInfo);
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed(false);
    }

    @Override
    public boolean onBackPressed(boolean fromKey) {
        saveAction();
        return super.onBackPressed(fromKey);
    }

    public void setResultCallBack(CreateTripDescFragment.OnResultCallback onResultCallback){
        this.onResultCallback=onResultCallback;
    }
}
